<!--
 * @Descripttion: 核酸宝项目前端
 * @version: 
 * @Author: 杜宇轩
 * @Date: 2022-05-22 05:23:33
 * @LastEditors: 杜宇轩
 * @LastEditTime: 2022-05-22 06:21:43
-->
<template>
  <div class="ApA_box">
    <div class="ApA_box_header">
      <i class="el-icon-menu"/>
      志愿者申请列表
    </div>
    <el-button
      class="ApA_batchButton"
      size="small"
      type="primary"
      @click="changeBatch"
    >
      批量操作
    </el-button>
    <el-button
      v-if="isBatch"
      class="ApA_denyButton"
      size="small"
      type="danger"
      @click="handleBatchDeny"
    >
      <i class="el-icon-close"/>
      批量拒绝
    </el-button>
    <el-button
      v-if="isBatch"
      class="ApA_passButton"
      type="success"
      size="small"
      @click="handleBatchPass"
    >
      <i class="el-icon-check"/>
      批量同意
    </el-button>
    <el-table
      class="ApA_table"
      :data="tableData"
      min-height="500px"
      :default-sort = "{prop: 'publishTime', order: 'descending'}"
      @selection-change="handleSelectionChange"
      highlight-current-row
      border
      stripe
    >
      <el-table-column
        v-if="isBatch"
        type="selection"
        width="40"
      />
      <el-table-column
        prop="name"
        label="用户昵称"
        width="120"
      />
      <el-table-column
        prop="volunteerPosition"
        label="志愿地点"
        min-width="180"
      />
      <el-table-column
        prop="volunteerTime"
        label="志愿时间"
        width="180"
      />
      <el-table-column
        prop="state"
        label="申请状态"
        width="120"
      />
      <el-table-column
        label="操作"
        width="120"
      >
        <template slot-scope="scope" v-if="!isBatch && scope.row.state === '待定'">
          <el-link
            type="success"
            style="margin-right: 10px;"
            :underline="false"
            @click="handlePass(scope.$index, scope.row)"
          >
            同意
          </el-link>
          <el-link
            type="danger"
            :underline="false"
            @click="handleDeny(scope.$index, scope.row)"
          >
            拒绝
          </el-link>
        </template>
      </el-table-column>
    </el-table>
  </div>
</template>

<script>
import {
  approverCheckApply,
  approverHandleApply
} from '@/api/approver/approverApply'
export default {
  name: 'ApproverApply',
  data() {
    return {
      originData: [],
      tableData: [],
      handleData: [],
      isBatch: false
    }
  },
  created() {
    this.$store.dispatch('region/getAllInfo', 0).then((regionData) => {
      approverCheckApply().then((data) => {
        data.forEach(item => {
          const obj = regionData.filter(d => d.regionId === item.volunteerLocation)[0];
          item.volunteerPosition = obj.province + obj.city + obj.area + obj.testLocation;
          this.originData.push(item);
        });
        this.tableData = this.originData;
      });
    });
  },
  methods: {
    changeBatch() {
      if (this.isBatch === false) {
        this.tableData = this.originData.filter(d => d.state === '待定');
        this.isBatch = true;
      } else {
        this.tableData = this.originData;
        this.isBatch = false;
      }
    },
    handleSelectionChange(val) {
      this.handleData = val;
    },
    handlePass(index, row) {
      const input = [];
      const obj = {};
      obj.id = row.id;
      obj.userId = row.userId;
      obj.state = "同意";
      input.push(obj);
      approverHandleApply(input).then(() => {
        this.$message.success('审批成功');
        row.state = "同意";
      });
    },
    handleDeny(index, row) {
      const input = [];
      const obj = {};
      obj.id = row.id;
      obj.userId = row.userId;
      obj.state = "拒绝";
      input.push(obj);
      approverHandleApply(input).then(() => {
        this.$message.success('审批成功');
        row.state = "拒绝";
      });
    },
    handleBatchPass() {
      const input = [];
      this.handleData.forEach(item => {
        const obj = {};
        obj.id = item.id;
        obj.userId = item.userId;
        obj.state = "同意";
        input.push(obj);
      });
      approverHandleApply(input).then(() => {
        this.$message.success('审批成功');
        this.handleData.forEach(item => {
          item.state = "同意";
        });
        this.changeBatch();
      });
    },
    handleBatchDeny() {
      const input = [];
      this.handleData.forEach(item => {
        const obj = {};
        obj.id = item.id;
        obj.userId = item.userId;
        obj.state = "拒绝";
        input.push(obj);
      });
      approverHandleApply(input).then(() => {
        this.$message.success('审批成功');
        this.handleData.forEach(item => {
          item.state = "拒绝";
        });
        this.changeBatch();
      });
    }
  }
}
</script>

<style lang="scss">
.ApA_box {
  background: #FFFFFF;
  display: inline-block;
  width: calc(100% - 100px);
  min-width: 805px;
  margin: 50px 50px 0px 50px;
  border-radius: 5px;
  box-shadow: 1px 1px 3px 3px rgba(0,0,0,0.2);

  .ApA_box_header {
    background: #EEE;
    padding: 5px;
    border-radius: 5px 5px 0px 0px;
    border-bottom: 1px solid #DDD;
  }

  .ApA_batchButton {
    margin: 10px 10px 10px 20px;
    float: left;
    font-size: 14px;
  }
  
  .ApA_passButton {
    margin: 10px 10px 10px 10px;
    float: right;
    font-size: 14px;
  }

  .ApA_denyButton {
    margin: 10px 20px 10px 10px;
    float: right;
    font-size: 14px;
  }

  .ApA_table {
    width: calc(100% - 40px);
    margin: 10px 20px 10px 20px;

    .ApA_table_expand {
      font-size: 0;
    }
    .ApA_table_expand label {
      width: 90px;
      color: #005187;
    }
    .ApA_table_expand .el-form-item {
      margin: 0px 0px 0px 10px;
      width: 60%;
    }
  }
}

</style>